﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.GUI.Library;
using QuanLyKho.DataAccess;
using QuanLyKho.Common;

namespace QuanLyKho.GUI
{
    public partial class frmListDistrict : Form
    {
        private static long ID = 0;
        public frmListDistrict()
        {
            InitializeComponent();
           
        }
        public frmListDistrict(long _ID)
        {
            InitializeComponent();
            txtCode.Focus();
            ID = _ID;
            binDataOneDistrict(_ID);
            
        }

        private void binDataOneDistrict(long _ID)
        {
            List<clsDistrict> listDistrict = new clsDataAccessListDistrict().ListDistrictProvince_SelectOneWithId(_ID);
            if (listDistrict.Count > 0)
            {
                txtCode.Text = listDistrict[0].Code;
                txtName.Text = listDistrict[0].Name;
                txtShortName.Text = listDistrict[0].ShortName;
                txtDes.Text = listDistrict[0].Des;
                cboIDListProvince.SelectedValue = listDistrict[0].Nameprovince;
            }
        }

        private void frmListDistrict_Load(object sender, EventArgs e)
        {
            LoadDatacombo();
        }


        public void LoadDatacombo()
        {
            try
            {
                List<clsProvince> listProvince = new clsDataAccessListProvince().ListProvinceSelectAll();
                DataTable dt = new DataTable("Province");
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("Name", typeof(string));
                DataRow dr;
                foreach (clsProvince obj in listProvince)
                {
                    dr = dt.NewRow();
                    dr[0] = obj.ID;
                    dr[1] = obj.Name;
                    dt.Rows.Add(dr);
                }
                cboIDListProvince.DataSource = dt;
               
                cboIDListProvince.DisplayMember = dt.Columns["Name"].ToString(); 
                 cboIDListProvince.ValueMember = dt.Columns["ID"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void butGhilai_Click(object sender, EventArgs e)
        {
            string Code = txtCode.Text;
            string Name = txtName.Text;
            string ShortName = txtShortName.Text;
            string Des = txtDes.Text;
            long IDListProvince = Convert.ToInt64(cboIDListProvince.SelectedValue);
            if (Code.Length < 1)
            {
                new clsLibrary().runLoading("Mã không được trống.", 0);
                txtCode.Focus();
                return;
            }
            else
            {
                if (Code.Length > 255)
                {
                    new clsLibrary().runLoading("Mã không lớn hơn 255 ký tự.", 0);
                    txtCode.Focus();
                    return;
                }
            }

            if (Name.Length < 1)
            {
                new clsLibrary().runLoading("Họ tên không được trống.", 0);
                txtName.Focus();
                return;
            }
            else
            {
                if (Name.Length > 255)
                {
                    new clsLibrary().runLoading("Họ tên không lớn hơn 255 ký tự.", 0);
                    txtName.Focus();
                    return;
                }
            }
            clsDistrict obj = new clsDistrict();
            obj.Code = Code;
            obj.Name = Name;
            obj.ShortName = ShortName;
            obj.Des = Des;
            obj.IDListProvince = IDListProvince;
            if (ID < 1)
            {
                if (new clsDataAccessListDistrict().AddNew(obj) > 0)
                {
                    new clsLibrary().runLoading("Đã thêm mới thành công:", 2);
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi thêm mới.", 0);
            }
            else
            {
                obj.ID = ID;
                if (new clsDataAccessListDistrict().Update(obj) > 0)
                {
                    new clsLibrary().runLoading("Đã sửa thành công:", 2);
                    refesh();
                    ID = 0;
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi sửa.", 0);
            }          
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void refesh() 
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtShortName.Text = "";
            txtDes.Text = "";        
        }

        private void butAddNew_Click(object sender, EventArgs e)
        {
            refesh();           
        }

    }
}
